package com.leejxx.health.service.impl;

import com.leejxx.health.dao.PermissionDao;
import com.leejxx.health.dao.RoleDao;
import com.leejxx.health.dao.UserDao;
import com.leejxx.health.pojo.Permission;
import com.leejxx.health.pojo.Role;
import com.leejxx.health.pojo.User;
import com.leejxx.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author LeeJx
 * @ClassName UserServiceImpl
 * @Description TODO
 * @date 2020/12/29 16:52
 * @Version 1.0
 */
@Slf4j
@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    @Transactional
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        List<Role> roles = roleDao.findById(user.getId());

        for (Role role : roles) {
            List<Permission> permissions = permissionDao.findById(role.getId());
            role.getPermissions().addAll(permissions);
        }
        user.getRoles().addAll(roles);
        return user;
    }
}
